package com.santiao.fuel.ui.fragment

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.santiao.fuel.R
import com.santiao.fuel.bean.SceienceItemData
import com.santiao.fuel.event.XXLEvent
import com.santiao.fuel.ui.activity.GasEquipActivity
import com.santiao.fuel.ui.activity.GasGuideDetails
import com.santiao.fuel.ui.activity.GasSafetyGuide
import com.santiao.fuel.ui.activity.Gas_price_contents
import com.santiao.fuel.ui.activity.gas_price_query
import com.santiao.fuel.ui.adapter.GasNewsAdapter
import com.santiao.fuel.ui.adapter.base.ItemClickListener
import com.santiao.fuel.utils.DisplayUtil
import com.santiao.publics.ad.ads.AsService
import com.santiao.publics.config.Const
import com.santiao.publics.util.LogUtil
import com.santiao.publics.util.SharedPreferencesUtil
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Collections


class MainFragment : Fragment(){

    private lateinit var charge_rv_recycle:RecyclerView
    private lateinit var charge_rl_price:RelativeLayout
    private lateinit var charge_rl_all:RelativeLayout
    private lateinit var home_container: FrameLayout
    private lateinit var charge_tv_selected:TextView
    var gasNewsList : ArrayList<SceienceItemData>  = ArrayList()
    var isReshXXL = false

    var mFeedContainer: FrameLayout? = null
    var mFeedContainer2: FrameLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        DisplayUtil.initActionBar(requireActivity())
        return inflater.inflate(R.layout.fragment_main,container,false)
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 注册全局事件分发
        if(!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        initView2(view)


    }


    private fun initView2(view: View){


        val gasUseLabel = view.findViewById<TextView>(R.id.gas_use_label)



        // 加载字体
//        val dinbTTF = Typeface.createFromAsset(requireContext().assets, "fonts/sansserif.ttf")
//
//        gasUseLabel.setTypeface(dinbTTF);

        mFeedContainer =view.findViewById(R.id.fl_content_1)
        mFeedContainer2 =view.findViewById(R.id.fl_content_11)

        val enterQueryBtn = view.findViewById<TextView>(R.id.charge_tv_selected)
        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)){

          enterQueryBtn.visibility = View.VISIBLE;
         }

        val price_wrapper = view.findViewById<LinearLayout>(R.id.price_wrapper)
        val equip_wrapper = view.findViewById<LinearLayout>(R.id.equip_wrapper)
        val guild_wrapper = view.findViewById<LinearLayout>(R.id.guild_wrapper)


        val  gas_news_list  = view.findViewById<RecyclerView>(R.id.charge_rv_recycle)


        val price_menu_name = price_wrapper.findViewById<TextView>(R.id.menu_name)
        val price_menu_icon = price_wrapper.findViewById<ImageView>(R.id.menu_icon)

        val equip_menu_name = equip_wrapper.findViewById<TextView>(R.id.menu_name)
        val equip_menu_icon = equip_wrapper.findViewById<ImageView>(R.id.menu_icon)

        val guild_menu_name = guild_wrapper.findViewById<TextView>(R.id.menu_name)
        val guild_menu_icon = guild_wrapper.findViewById<ImageView>(R.id.menu_icon)

        price_menu_name.text = "燃气价格"
        equip_menu_name.text = "燃气设备表"
        guild_menu_name.text = "燃气使用指南"

        price_menu_icon.setBackgroundResource(R.mipmap.gas_price)
        equip_menu_icon.setBackgroundResource(R.mipmap.gas_detail)
        guild_menu_icon.setBackgroundResource(R.mipmap.gas_use)


        initGasList();



        // 设置recycleView布局管理器
        gas_news_list.layoutManager = LinearLayoutManager(requireActivity());

        var chargeAdapter=  GasNewsAdapter(requireActivity(),R.layout.gas_news_item,gasNewsList)
        gas_news_list.adapter=chargeAdapter

        (gas_news_list.adapter as GasNewsAdapter).setItemClickListener(
            object : ItemClickListener<SceienceItemData> {
                override fun click(position: Int, data: SceienceItemData) {
                    val intent = Intent(requireContext(), GasGuideDetails::class.java)
                    intent.putExtra(GasGuideDetails.PAGE_TITLE , data.title)
                    intent.putExtra(GasGuideDetails.PAGE_DESC , data.descripte)
                    startActivity(intent)
                }

            })

        equip_wrapper.setOnClickListener {
            GasEquipActivity.comeGasEquipActivity(requireActivity())
        }


        guild_wrapper.setOnClickListener {
            GasSafetyGuide.comeGasSafetyGuideActivity(requireActivity())
        }



        enterQueryBtn.setOnClickListener {
            startActivity(Intent(requireContext() , gas_price_query::class.java))
        }


        price_wrapper.setOnClickListener {
            startActivity(Intent(requireContext() , Gas_price_contents::class.java))
        }

        adLoad(1);
    }


    /**
     * 初始化燃气科普列表的数据
     */
    private fun initGasList() {



        Collections.addAll(
            gasNewsList,


            SceienceItemData(
                R.mipmap.news_icon,
                "燃气安全基础知识",
                resources.getString(R.string.wz_61)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气设备的选择与安装",
                resources.getString(R.string.wz_7)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气设备的使用和维护",
                resources.getString(R.string.wz_3)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气泄漏应急处理",
                resources.getString(R.string.wz_4)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气安全点检测",
                resources.getString(R.string.rq_anquanjiance)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气设备故障排除",
                resources.getString(R.string.rq_shebeiguzhang)
            ),
            SceienceItemData(
                R.mipmap.news_icon,
                "燃气使用中的节能技巧",
                resources.getString(R.string.wz_9)
            ),
        )
    }


    override fun onResume() {
        super.onResume()

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageSimple(t: XXLEvent) {

    }



    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    fun adLoad( position:Int ){
        val ad2 = SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD_2) //自然流量广告模式

        LogUtil.i(Const.TAG,"MainActivity---------------adLoad----------------"+position)
        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD) || (position == 0 && ad2) ) {


            val adservice = AsService()
            var mFeedContainer: FrameLayout? = mFeedContainer
            var mFeedContainer2: FrameLayout? = mFeedContainer2

            var feedId = resources.getString(R.string.feed_native_media_id)
            var feedId2 = resources.getString(R.string.feed_native_media_2_id)
//            if (0 == position) {
//                mFeedContainer = stackFragment.elementAt(0).activity?.findViewById(R.id.fl_content_1);
////                    mFeedContainer2 = mainFragment.activity?.findViewById(R.id.fl_content_11);
//            }  else if (1 == position) {
//                mFeedContainer = stackFragment.elementAt(1).activity?.findViewById(R.id.fl_content_2);
////                mFeedContainer2 = waterFragment.activity?.findViewById(R.id.fl_content_11);
//            }else  if  (2 == position) {
//                mFeedContainer = stackFragment.elementAt(2).activity?.findViewById(R.id.fl_content_3);
////                  mFeedContainer2 = fortuneFragment.fl_content_22;
//            }
            adservice.initAd(
                "feed",
                context,
                feedId,
                mFeedContainer
            )

            adservice.showAd("feed", activity)


            if(mFeedContainer2!=null) {

                adservice.initAd(
                    "feed2",
                    context,
                    feedId2,
                    mFeedContainer2
                )

                adservice.showAd("feed2", activity)
            }

//
//            if (!MyStringUtil.dayIfAdOne(position.toString())) {
//                MyStringUtil.setDayIfAdOne(position.toString())


//
//            //插屏
//
//            adservice.initAd("full", context, resources.getString(R.string.full_media_id), null)
//
//
//            ////            //插屏
//
//
//            //显示插屏
//            Handler().postDelayed({ adservice.showAd("full", activity) }, 1500)
//
//            if (!ad2) {
////                if (position == -1) {
//
//                //首次 双插屏
////                val isFirstLaunchApp: Boolean =
////                    SharedPreferencesUtil.getStatus("first_launch_list_fragment")
////                if (!isFirstLaunchApp && Const.shuangInterstitialFull || position!=0) {
//                adservice.initAd(
//                    "full2", context, resources.getString(R.string.full_media_2_id),
//                    null
//                )
//
//
//                Handler().postDelayed({adservice.showAd("full2", activity) }, 1500)
//
//                SharedPreferencesUtil.saveStatus("first_launch_list_fragment", true);
////                }
//
//            }
////            }

        }

    }

}
